Chapter 30 - Demo: Static Website Part 2
Converting the Terraform code to a reusable module.
Overview
You'll add two files when creating this module
- license
- readme
Module Folder Structure
An example can be found here: https://github.com/Azure/terraform-azurerm-vnet
- Create
modules
folder - Create
azure-static-website
folder nested inside that. - Copy your
main.tf
,outputs.tf
,variables.tf
, and yourversions.tf
files into this folder (or start from scratch)
main.tf
# Provider Block
provider "azurerm" {
features {}
}
# Random String Resource
resource "random_string" "myrandom" {
length = 6
upper = false
special = false
number = false
}
# Create Resource Group
resource "azurerm_resource_group" "resource_group" {
name = var.resource_group_name
location = var.location
}
# Create Azure Storage account
resource "azurerm_storage_account" "storage_account" {
name = "${var.storage_account_name}${random_string.myrandom.id}"
resource_group_name = azurerm_resource_group.resource_group.name
location = var.location
account_tier = var.storage_account_tier
account_replication_type = var.storage_account_replication_type
account_kind = var.storage_account_kind
static_website {
index_document = var.static_website_index_document
error_404_document = var.static_website_error_404_document
}
}
outputs.tf
# Output variable definitions
output "resource_group_id" {
description = "resource group id"
value = azurerm_resource_group.resource_group.id
}
output "resource_group_name" {
description = "The name of the resource group"
value = azurerm_resource_group.resource_group.name
}
output "resource_group_location" {
description = "resource group location"
value = azurerm_resource_group.resource_group.location
}
output "storage_account_id" {
description = "storage account id"
value = azurerm_storage_account.storage_account.id
}
output "storage_account_name" {
description = "storage account name"
value = azurerm_storage_account.storage_account.name
}
variables.tf
# Input variable definitions
variable "location" {
description = "The Azure Region in which all resources groups should be created."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group"
type = string
}
variable "storage_account_name" {
description = "The name of the storage account"
type = string
}
variable "storage_account_tier" {
description = "Storage Account Tier"
type = string
}
variable "storage_account_replication_type" {
description = "Storage Account Replication Type"
type = string
}
variable "storage_account_kind" {
description = "Storage Account Kind"
type = string
}
variable "static_website_index_document" {
description = "static website index document"
type = string
}
variable "static_website_error_404_document" {
description = "static website error 404 document"
type = string
}
versions.tf
# Terraform Block
terraform {
required_version = ">= 1.9.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "< 4.0"
}
random = {
source = "hashicorp/random"
version = ">= 3.0"
}
}
}
- Add in your license file and readme file
License
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Readme
This is my readme file.
# TF Version
---
TF = 1.9.0
AzureRM = < 4.0.
# Inputs
---
location
resource_group_name
storage_account_name
etc....
# Outputs
Update module code - root directory
These files go into your terraform root directory, not the modules folder.
- Create your versions.tf file.
# Versions.tf
terraform {
required_version = ">= 1.9.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "< 4.0"
}
}
}
# Provider Block
provider "azurerm" {
features {}
}
- Create your variables file We will leave this blank for now
# Variables
- Create your resource-static-website.tf You will want to put these inputs inside your Readme file.
module "azure_static_website" {
source = "./modules/azure-static-website" ## <- folder inside modules
# Resource Group
location = "eastus"
resource_group_name = "myrg1"
# Storage Account
storage_account_name = "staticwebsite"
storage_account_tier = "Standard"
storage_account_replication_type = "LRS"
storage_account_kind = "StorageV2"
static_website_index_document = "index.html"
static_website_error_404_document = "error.html"
}
- Define your outputs These may come from your existing code - or, you may want to define others.
- Change your values from the outputs - these will now be
module.nameofmodule.resource.attribute
output "root_resource_group_id" {
description = "resource group id"
value = module.azure_static_website.resource_group_id
}
output "root_resource_group_name" {
description = "The name of the resource group"
value = module.azure_static_website.resource_group_name
}
output "root_resource_group_location" {
description = "resource group location"
value = module.azure_static_website.resource_group_location
}
output "root_storage_account_id" {
description = "storage account id"
value = module.azure_static_website.storage_account_id
}
output "root_storage_account_name" {
description = "storage account name"
value = module.azure_static_website.storage_account_name
}
Terraform init, validate, plan, apply
- Go ahead and run your terraform commands to build the infrastructure.
- Notice that you still need to upload your index.html and your 404.html pages, but static website is enabled.
Clean up
- Run
terraform destroy
- Clean up any manually created resources from the first part
Terraform Get
Use terraform get
to get the local modules and install them. You can use terraform init
with this to get your entire terraform working space ready
Note: Local modules are kept in the source directory while remote modules are downloaded to the .terraform/modules directory.
Local modules are automatically updated and loaded due to this, no need to run a terraform get
or terraform init